﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using TagPropagator;

namespace PluszowyMis
{
    class Program
    {
        static void Main(string[] args)
        {
            String annotationsPath = "C:\\Users\\bartek\\Pictures\\Focie na wzaip\\mirflickr08_annotations_v080";
            String irr = "C:\\Users\\bartek\\Pictures\\Focie na wzaip\\";
            String[] dirs = new String[] 
            { 
                "mirflickr08_images1\\images",
                //"mirflickr08_images2\\images",
                //"mirflickr08_images3\\images",
                //"mirflickr08_images4\\images",
                //"mirflickr08_images5\\images"
            };

            Dictionary<String, HashSet<String>> tags = new Dictionary<string, HashSet<string>>();

            //foreach (var file in Directory.GetFiles(annotationsPath))
            //{
            //    if (!file.Contains(".txt")) continue;
            //    var split = file.Split('.')[0].Split('\\');
            //    var tagName = split[split.Length - 1];
            //    List<String> pics = new List<string>();
            //    tags.Add(tagName, pics);

            //    using (TextReader tr = new StreamReader(file))
            //    {
            //        String s = tr.ReadLine();
            //        while (!String.IsNullOrEmpty(s))
            //        {
            //            pics.Add(s);
            //            s = tr.ReadLine();
            //        }
            //    }
            //}

            foreach (var file in Directory.GetFiles(annotationsPath))
            {
                if (!file.Contains(".txt")) continue;
                var split = file.Split('.')[0].Split('\\');
                var tagName = split[split.Length - 1];

                using (TextReader tr = new StreamReader(file))
                {
                    String s = tr.ReadLine();
                    while (!String.IsNullOrEmpty(s))
                    {
                        string name = "im" + s + ".jpg";
                        if (!tags.ContainsKey(name)) tags.Add(name, new HashSet<string>());
                        tags[name].Add(tagName);
                        s = tr.ReadLine();
                    }
                }
            }

            Graph graph = new Graph();
            foreach (var dir in dirs)
            {
                foreach (var picture in Directory.GetFiles(irr + dir))
                {
                    string tmp = picture.Split('.')[0];
                    var tmpTab = picture.Split('\\');
                    tmp = tmpTab[tmpTab.Length - 1];

                    Node n = new Node()
                    {
                        Image = picture
                    };

                    if (tags.ContainsKey(tmp))
                    {
                        foreach (var tag in tags[tmp])
                        {
                            n.Tags.Add(tag, 1.0);
                        }
                    }
                    graph.Nodes.Add(n);
                }
            }

            Serializer ser = new Serializer();
            ser.Serialize(graph, "C:\\5kzdjecBezSasiadow.xml");

        }
    }
}
